# # -*- coding:utf-8 -*-
# Author：wancong
# Date: 2018-04-30
from pyhanlp import *


def demo_pinyin():
    # """ 汉字转拼音
    #
    # >>> demo_pinyin()
    # 原文， 重载不是重任！
    # 拼音（数字音调）， [chong2, zai3, bu2, shi4, zhong4, ren4, none5]
    # 拼音（符号音调）， chóng, zǎi, bú, shì, zhòng, rèn, none,
    # 拼音（无音调）， chong, zai, bu, shi, zhong, ren, none,
    # 声调， 2, 3, 2, 4, 4, 4, 5,
    # 声母， ch, z, b, sh, zh, r, none,
    # 韵母， ong, ai, u, i, ong, en, none,
    # 输入法头， ch, z, b, sh, zh, r, none,
    # jie zhi none none none none nian none
    # jie zhi 2 0 1 2 nian ，
    # """
    Pinyin = JClass("com.hankcs.hanlp.dictionary.py.Pinyin")
    text = "重载不是重任！"
    pinyin_list = HanLP.convertToPinyinList(text)

    print("原文，", end=" ")
    print(text)
    print("拼音（数字音调），", end=" ")
    print(pinyin_list)
    print("拼音（符号音调），", end=" ")
    for pinyin in pinyin_list:
        print("%s," % pinyin.getPinyinWithToneMark(), end=" ")
    print("\n拼音（无音调），", end=" ")
    for pinyin in pinyin_list:
        print("%s," % pinyin.getPinyinWithoutTone(), end=" ")
    print("\n声调，", end=" ")
    for pinyin in pinyin_list:
        print("%s," % pinyin.getTone(), end=" ")
    print("\n声母，", end=" ")
    for pinyin in pinyin_list:
        print("%s," % pinyin.getShengmu(), end=" ")
    print("\n韵母，", end=" ")
    for pinyin in pinyin_list:
        print("%s," % pinyin.getYunmu(), end=" ")
    print("\n输入法头，", end=" ")
    for pinyin in pinyin_list:
        print("%s," % pinyin.getHead(), end=" ")

    print()
    # 拼音转换可选保留无拼音的原字符
    print(HanLP.convertToPinyinString("截至2012年，", " ", True))
    print(HanLP.convertToPinyinString("截至2012年，", " ", False))


def demo_pinyin_to_chinese():
    # """ HanLP中的数据结构和接口是灵活的，组合这些接口，可以自己创造新功能
    #
    # >>> demo_pinyin_to_chinese()
    # [renmenrenweiyalujiangbujian/null, lvse/[滤色, 绿色]]
    # """
    AhoCorasickDoubleArrayTrie = JClass(
        "com.hankcs.hanlp.collection.AhoCorasick.AhoCorasickDoubleArrayTrie")
    StringDictionary = JClass(
        "com.hankcs.hanlp.corpus.dictionary.StringDictionary")
    CommonAhoCorasickDoubleArrayTrieSegment = JClass(
        "com.hankcs.hanlp.seg.Other.CommonAhoCorasickDoubleArrayTrieSegment")
    CommonAhoCorasickSegmentUtil = JClass(
        "com.hankcs.hanlp.seg.Other.CommonAhoCorasickSegmentUtil")
    Config = JClass("com.hankcs.hanlp.HanLP$Config")

    TreeMap = JClass("java.util.TreeMap")
    TreeSet = JClass("java.util.TreeSet")

    dictionary = StringDictionary()
    dictionary.load(Config.PinyinDictionaryPath)
    entry = {}
    m_map = TreeMap()
    for entry in dictionary.entrySet():
        pinyins = entry.getValue().replace("[\\d,]", "")
        words = m_map.get(pinyins)
        if words is None:
            words = TreeSet()
            m_map.put(pinyins, words)
        words.add(entry.getKey())
    words = TreeSet()
    words.add("绿色")
    words.add("滤色")
    m_map.put("lvse", words)

    segment = CommonAhoCorasickDoubleArrayTrieSegment(m_map)
    print(segment.segment("renmenrenweiyalujiangbujianlvse"))


# if __name__ == "__main__":
#     import doctest
#     doctest.testmod(verbose=True, optionflags=doctest.NORMALIZE_WHITESPACE)

# demo_pinyin()
demo_pinyin_to_chinese()
